package com.cgbl.service.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.HomeTeamAdapter
import com.cgbl.service.databinding.FragmentHomeBinding
import com.cgbl.service.vm.HomeViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.model.TeamListBean
import com.robot.base.util.arouter.ARouterUtil

class HomeFragment : GLBaseFragment<FragmentHomeBinding, HomeViewModel>() {

    lateinit var homeTeamAdapter: HomeTeamAdapter
    override fun initContentView(
        inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?
    ): Int = R.layout.fragment_home

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        homeTeamAdapter = HomeTeamAdapter()
        binding.recyclerView.apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = homeTeamAdapter
        }
        homeTeamAdapter.setOnItemClickListener { _, _, position ->
            ARouterUtil.goToActivityWithBundle(
                AppConstants.Router.Project.A_ADD_TEAM,
                Bundle().apply {
                    putString(AppConstants.BundleKey.ID, homeTeamAdapter.data.get(position).id)
                })
        }
        viewModel.fetchHomeTeam()
    }

    override fun initViewObservable() {
        viewModel.uc.homeTeamDataEvent.observe(this){
            homeTeamAdapter.setNewInstance(it as MutableList<TeamListBean>?)
        }
    }
}